package com.htdata.iiot.api.tsdb;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

import com.htdata.iiot.api.tsdb.bean.DataPoint;
import com.htdata.iiot.api.tsdb.bean.DeviceNumber;
import com.htdata.iiot.api.tsdb.bean.Metric;
import com.htiiot.common.config.Configured;

import net.opentsdb.core.Query;

/**
 * Created by SunMy on 2017/7/6.
 */

public class QueryTSDB extends Configured {

	/**
	 * 根据metricName,startTime,endTime,downsample获取数据
	 * 
	 * @param metricName
	 *            测点名称
	 * @param startTime
	 *            开始时间
	 * @param endTime
	 *            结束时间
	 * @param downsample
	 *            聚类算法,比如 3s-avg-zero
	 * @return
	 * @throws Exception
	 */
	public DataPoint[] getTSData(String metricName, long startTime, long endTime, String downsample) throws Exception {

		return getTSData(metricName, new Date(startTime), new Date(endTime), downsample);
	}

	/**
	 * 根据metricName,startTime,endTime,downsample获取数据
	 * 
	 * @param metricName
	 *            测点名称
	 * @param startTime
	 *            开始时间
	 * @param endTime
	 *            结束时间
	 * @param downsample
	 *            聚类算法,比如 3s-avg-zero
	 * @return
	 * @throws Exception
	 */
	public DataPoint[] getTSData(String metricName, Date startTime, Date endTime, String downsample) throws Exception {

		return getDataFromQuery(metricName, startTime, endTime, downsample);
	}

	/**
	 * get result from tsdb query
	 *
	 * @param tsdbqueries
	 *            array list of {@link Query}
	 * @return {@link DataPoint} arrays
	 */
	@SuppressWarnings("deprecation")
	private DataPoint[] getDataFromQuery(String metricName, Date startTime, Date endTime, String downsample) {
		List<DataPoint> dpResult = new ArrayList<DataPoint>();
		long startTimeL = startTime.getTime() / 1000 * 1000;
		long endTimeL = endTime.getTime() / 1000 * 1000;
		int interval = 1;
		if (downsample != null && downsample.length() != 0) {
			String intervalStr = downsample.split("-")[0];
			interval = Integer.parseInt(intervalStr.replace("s", "").replace("m", "").replace("h", ""));
			if (intervalStr.endsWith("s")) {
				interval = interval * 1000;
			} else if (intervalStr.endsWith("m")) {
				interval = interval * 1000 * 60;
			} else if (intervalStr.endsWith("h")) {
				interval = interval * 1000 * 60 * 60;
			}
		}
		String dn = metricName.split("_")[0];
		String name = metricName.split("_")[1];
		DeviceNumber deviceNumber = DeviceNumber.fromHexString(dn);
		//Random random = new Random();
		//int hashCode = metricName.hashCode();
		startTimeL = startTimeL-startTimeL%interval;
		
		
		long baseValue = deviceNumber.getTenantIdInt()%13+deviceNumber.getDeviceIdInt()%17+deviceNumber.getMetricIdLong()*17*13;
		if(baseValue>100){
			baseValue = baseValue%97;
		}
		for (; startTimeL < endTimeL; startTimeL = startTimeL + interval) {

			DataPoint datapoint = new DataPoint();
			datapoint.setDeviceNumber(deviceNumber);
			Metric metric = new Metric();
			metric.setName(name);
			double v = startTimeL*deviceNumber.getMetricIdLong()/1000*13%199*0.02;
			if(startTimeL/1000%19==0){
				metric.setValue(Math.abs(baseValue * 0.5-v));
			}else{
				metric.setValue(baseValue * 0.5+v);
			}
			datapoint.setTs(startTimeL);
			datapoint.setMetric(metric);
			dpResult.add(datapoint);

		}
		return dpResult.toArray(new DataPoint[0]);
	}

}
